package com.boot.springbootsystem.contoller;

import cn.dev33.satoken.stp.StpUtil;
import com.boot.springbootsystem.pojo.UserResult;
import com.boot.springbootsystem.pojo.OptionResult;
import com.boot.springbootsystem.pojo.User;
import com.boot.springbootsystem.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {

    // 依赖注入
    @Autowired
    UserMapper userMapper;

    @RequestMapping("/login")
    public OptionResult login() {
        return new OptionResult(200, "login");
    }

    @RequestMapping("/do_login")
    public UserResult doLogin(String email, String password) {
        // select * from user where email='21312@qq.com' and password='1'
        User user = userMapper.findByEmailPassword(email, password);
        UserResult loginResult = new UserResult(0, "login error", null);
        if(user!=null){
            StpUtil.login(user.getId());
            loginResult.setCode(200);
            loginResult.setMsg("login success");
            loginResult.setUser(user);
        }
        return loginResult;
    }

    @RequestMapping("/register")
    public OptionResult register() {
        return new OptionResult(200, "register");
    }

    @RequestMapping("/register_save")
    public UserResult registerSave(User user) {
        UserResult registerResult = new UserResult(0, "register error", null);
        Integer result = userMapper.insert(user);
        if(result>0){
            registerResult.setCode(200);
            registerResult.setMsg("register success");
            registerResult.setUser(user);
        }
        return registerResult;
    }

    @RequestMapping("/concat")
    public OptionResult concat() {
        return new OptionResult(200, "concat");
    }

}
